package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.achievement.service.CommentService;
import com.lanou3g.ptp.achievement.service.LikeService;
import com.lanou3g.ptp.entity.*;
import com.lanou3g.ptp.sso.service.SsoService;
import com.lanou3g.ptp.task.service.TaskService;
import com.lanou3g.ptp.user.service.UserService;
import com.lanou3g.ptp.vo.TaskVO;
import com.lanou3g.utils.PtpResult;
import com.lanou3g.ptp.achievement.service.DiaryService;
import com.lanou3g.utils.PtpResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "日志接口")
@RestController
public class DiaryController {

    @Reference
    private DiaryService diaryService;

    @Reference
    private TaskService taskService;

    @Reference
    private UserService userService;

    @Reference
    private LikeService likeService;

    @Reference
    private CommentService commentService;

    @Reference
    private SsoService ssoService;

    @ApiOperation("通过id查询日志信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "diaryId")
    })
    @GetMapping("/diary/{id}")
    public PtpResult findDiaryById(@PathVariable Long id) {

        Diary diary = diaryService.findDiaryById(id);
        Task task = taskService.findById(diary.getTaskId());
        User user = userService.userById(diary.getStudentId());
        String img = diary.getImg();
        //分割字符串
        String[] imgs;
        String meter = ",";
        imgs = img.split(meter);
        Map<String, Object> result = new HashMap<>();
        result.put("id", diary.getId());
        result.put("task_id", task.getName());
        result.put("student_id", user.getName());
        result.put("content", diary.getContent());
        result.put("status", diary.getStatus());
        result.put("imgs",imgs);
        return PtpResultUtils.ok(result);

    }


    @ApiOperation("查询所有日志信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value = "页数"),
            @ApiImplicitParam(name="size",value = "显示条数"),
            @ApiImplicitParam(name="keyword",value = "查询条件")
    })
    @GetMapping("/diarys")
    public PtpResult findAllDiary(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "size", defaultValue = "30") Integer size,
                                  String keyword) {

        PageInfo<Diary> p;
        try {
            if (keyword != null && !keyword.trim().equals("")) {
                p = diaryService.findByKeyword(page, size, keyword);
//                HashMap<String, Object> result = new HashMap<>();
//                List<Diary> list = p.getList();
//                for (Diary diary : list) {
//                    String img = diary.getImg();
//                    //分割字符串
//                    String[] imgs;
//                    String meter = ",";
//                    imgs = img.split(meter);
//                    result.put("diary",diary);
//                    result.put("imgs",imgs);
//                }
            } else {
                p = diaryService.findAllDiary(page, size);
            }
            return PtpResultUtils.ok(p);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    @ApiOperation("学生通过id修改日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "日志id")
    })
    @PutMapping("/diary/{id}")
    public PtpResult myUpdateDiary(@PathVariable Long id, Diary diary, String token) {
        Student student = (Student) ssoService.tokenUser(token);
        diary.setStudentId(student.getId());
        try {
            diaryService.updateDiary(id, diary);
            return PtpResultUtils.ok(200);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生添加日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content",value = "日志内容"),
            @ApiImplicitParam(name = "imgs",value = "图片"),
            @ApiImplicitParam(name = "taskId",value = "任务id"),
            @ApiImplicitParam(name = "token",value = "用户信息key值")
    })
    @PostMapping("/myDiary")
    public PtpResult myInsertDiary(String content,String[] imgs, Long taskId, String token) {
        Student student = (Student) ssoService.tokenUser(token);
        Diary diary = new Diary();
        diary.setContent(content);
        diary.setStatus(0);
        diary.setStudentId(student.getId());
        diary.setTaskId(taskId);
        //处理图片
        String str = "";
        for (String img : imgs) {
            str = str + img + ",";
        }
        //去掉最后一个逗号
        str = str.substring(0, str.length() - 1);
        diary.setImg(str);
        try {
            diaryService.insertDiary(diary);
            return PtpResultUtils.ok(200);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }


    @ApiOperation("通过id删除日志信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "日志id")
    })
    @DeleteMapping("/diary/{id}")
    public PtpResult deleteDiaryId(@PathVariable Long id) {

        try {
            diaryService.deleteDiaryId(id);
            return PtpResultUtils.ok(200);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    @ApiOperation("批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多个日志id")
    })
    @DeleteMapping("/diaries/{ids}")
    public PtpResult deleteDiaryIds(@PathVariable Long[] ids) {

        try {
            diaryService.deleteDiaryIds(ids);
            return PtpResultUtils.ok(200);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生取消点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "diaryId", value = "日志id"),
            @ApiImplicitParam(name = "token",value = "用户信息key值")
    })
    @DeleteMapping("/myLike/{diaryId}")
    public PtpResult deleteLike(@PathVariable Long diaryId, String token) {
        Student student = (Student) ssoService.tokenUser(token);
        try {
            likeService.delete(diaryId, student.getId());
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "日志id"),
            @ApiImplicitParam(name = "token",value = "用户信息key值")
    })
    @PostMapping("/myLike")
    public PtpResult insert(Long id, String token) {
        Student student = (Student) ssoService.tokenUser(token);
        Like like = new Like();
        like.setStudentId(student.getId());
        like.setDiaryId(id);
        like.setStudentId(student.getId());
        try {
            likeService.insert(like);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生按日志id查询点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "日志Id")
    })
    @GetMapping("/myLike/{id}")
    public PtpResult likeFindByDiaryId(@PathVariable Long id) {
        try {
            Long like = likeService.find(id);
            return PtpResultUtils.ok(like);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }
//    @ApiOperation("查询学生当天日志")
//    @GetMapping("/allDiaries")
//    public PtpResult findByTime(){
//
//    }
    @ApiOperation("学生删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "likeId")
    })
    @DeleteMapping("/myComment/{id}")
    public PtpResult deleteComment(@PathVariable Long diaryId, String token) {
        try {
            Student student = (Student) ssoService.tokenUser(token);
            commentService.delete(diaryId, student.getId());
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生添加评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "日志id"),
            @ApiImplicitParam(name = "comment", value = "评论"),
            @ApiImplicitParam(name = "token",value = "用户信息key值")
    })
    @PostMapping("/myComment")
    public PtpResult insert(Long id, String comment, String token) {
        try {
            Student student = (Student) ssoService.tokenUser(token);
            Comment comment1 = new Comment();
            comment1.setComment(comment);
            comment1.setDiaryId(id);
            comment1.setStudentId(student.getId());
            commentService.insert(comment1);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生按日志id查询评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "DiaryId")
    })
    @GetMapping("/myComment/{id}")
    public PtpResult commentFindByDiaryId(@PathVariable Long id) {
        try {
            List<Comment> comments = commentService.find(id);
            return PtpResultUtils.ok(comments);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生查询所有日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户信息key值")
    })
    @GetMapping("/myDiarys")
    public PtpResult myFindAll(String token) {
        try {
            Student student = (Student) ssoService.tokenUser(token);
            List<Diary> diaries = diaryService.MyFindAll(student.getId());
            return PtpResultUtils.ok(diaries);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }
}
